package org.apache.sad.core.framework.logging.log4j;

import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;

import org.apache.sad.core.framework.logging.auditing.types.Aspect;

public class MethodConfiguration {
	public static final String MARKET_FIELD_TYPE = "MARKET";
	public static final String LOCATION_FIELD_TYPE = "LOCATION";
	public static final String ACCOUNT_FIELD_TYPE = "ACCOUNT";
	public static final String CUSTOMER_FIELD_TYPE = "CUSTOMER";
	
	public static final String BASE64_ENCRYPTION = "BASE64";
	
	private String txnKeyExpression;
	private String marketFieldExpression;
	private String marketFieldType;
	private String marketFieldEncryption;
	private List<Aspect> aspects = new ArrayList<Aspect>();
	private String name;
	private int percentageLogging;
	private Random random;
	
	public List<Aspect> getAspects() {
		return aspects;
	}

	public void setAspects(List<Aspect> aspects) {
		this.aspects = aspects;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTxnKeyExpression() {
		return txnKeyExpression;
	}

	public void setTxnKeyExpression(String txnKeyExpression) {
		this.txnKeyExpression = txnKeyExpression;
	}
	
	public String getMarketFieldExpression() {
		return marketFieldExpression;
	}

	public void setMarketFieldExpression(String marketFieldExpression) {
		this.marketFieldExpression = marketFieldExpression;
	}
	
	public String getMarketFieldType() {
		return marketFieldType;
	}

	public void setMarketFieldType(String marketFieldType) {
		this.marketFieldType = marketFieldType;
	}

	public String getMarketFieldEncryption() {
		return marketFieldEncryption;
	}

	public void setMarketFieldEncryption(String marketFieldEncryption) {
		this.marketFieldEncryption = marketFieldEncryption;
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof MethodConfiguration))
			return false;
		MethodConfiguration mc = (MethodConfiguration)o;
		return (this.getName().equals(mc.getName()));
	}
	
	public void setAspectNames(String aspectNames) {
		StringTokenizer st = new StringTokenizer(aspectNames, ",");
		if (this.aspects.size() == 0) {
			while (st.hasMoreTokens()) {
				String aspectName = st.nextToken().trim();
				Aspect aspect = new Aspect();
				aspect.setName(aspectName);
				this.aspects.add(aspect);
			}
		}
		else {
			int i = 0; 
			while (st.hasMoreTokens()) {
				String aspectName = st.nextToken().trim();
				this.aspects.get(i++).setName(aspectName);	
			}
		}
	}
	
	public void setAspectExpressions(String aspectExpressions) {
		StringTokenizer st = new StringTokenizer(aspectExpressions, ",");		
		if (this.aspects.size() == 0) {
			while (st.hasMoreTokens()) {
				String aspectExpression = st.nextToken().trim();
				Aspect aspect = new Aspect();
				aspect.setExpr(aspectExpression);
				this.aspects.add(aspect);
			}
		}
		else {
			int i = 0; 
			while (st.hasMoreTokens()) {
				String aspectExpression = st.nextToken().trim();
				this.aspects.get(i++).setExpr(aspectExpression);
			}
		}		
	}

	public int getPercentageLogging() {
		return percentageLogging;
	}

	public void setPercentageLogging(int percentageLogging) {
		this.percentageLogging = percentageLogging;
		this.random=new Random();
	}

	public Random getRandom() {
		return random;
	}

}